package com.gitee.qdbp.general.mgr.controller.trade;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.able.jdbc.ordering.OrderPaging;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.result.ResponseMessage;
import com.gitee.qdbp.base.annotation.OperateRecord;
import com.gitee.qdbp.base.enums.OperateType;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.utils.SessionTools;
import com.gitee.qdbp.general.common.api.settings.service.IAreaDivisionQueryer;
import com.gitee.qdbp.general.trade.api.personal.model.PersonalAddressBase;
import com.gitee.qdbp.general.trade.api.personal.model.PersonalAddressBean;
import com.gitee.qdbp.general.trade.api.personal.model.PersonalAddressWhere;
import com.gitee.qdbp.general.trade.api.personal.service.IPersonalAddressExecutor;
import com.gitee.qdbp.general.trade.api.personal.service.IPersonalAddressQueryer;
import com.gitee.qdbp.tools.utils.ConvertTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 个人收货地址管理控制器
 *
 * @author zhaohuihua
 * @version 180628
 */
@Controller
@RequestMapping("actions/my-address")
public class MyAddressController {

    @Autowired
    private IPersonalAddressQueryer personalAddressQueryer;
    @Autowired
    private IPersonalAddressExecutor personalAddressExecutor;
    @Autowired
    private IAreaDivisionQueryer areaDivisionQueryer;

    @ResponseBody
    @RequestMapping("list")
    @OperateRecord(value = "个人收货地址:查询", type = OperateType.QUERY)
    public ResponseMessage list(PersonalAddressWhere model, OrderPaging paging) {
        try {
            IAccount me = SessionTools.getLoginUser();
            model.setUserId(me.getId());
            PageList<PersonalAddressBean> list = personalAddressQueryer.list(model, paging);
            return new ResponseMessage(list);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    @ResponseBody
    @RequestMapping("create")
    @OperateRecord(value = "个人收货地址:新增", type = OperateType.CREATE)
    public ResponseMessage create(PersonalAddressBase model, Boolean isDefault) {
        if (isDefault == null) isDefault = false;
        if (VerifyTools.isNotBlank(model.getAreaCode())) {
            // 由于地区编号经常会变, 需要保存地区名称
            String areaText = areaDivisionQueryer.findAreaText("default", model.getAreaCode());
            model.setAreaText(areaText);
        }
        try {
            IAccount me = SessionTools.getLoginUser();
            String id = personalAddressExecutor.create(model, me.getId(), isDefault, me);
            Map<String, Object> map = new HashMap<>();
            map.put("id", id);
            return new ResponseMessage(map);
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    @ResponseBody
    @RequestMapping("update")
    @OperateRecord(value = "个人收货地址:修改", type = OperateType.UPDATE)
    public ResponseMessage update(String id, PersonalAddressBase model) {
        if (VerifyTools.isNotBlank(model.getAreaCode())) {
            // 由于地区编号经常会变, 需要保存地区名称
            String areaText = areaDivisionQueryer.findAreaText("default", model.getAreaCode());
            model.setAreaText(areaText);
        }
        try {
            IAccount me = SessionTools.getLoginUser();
            personalAddressExecutor.update(id, model, me);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    @ResponseBody
    @RequestMapping("set-default")
    @OperateRecord(value = "个人收货地址:设置默认地址", type = OperateType.UPDATE)
    public ResponseMessage setDefault(String id) {
        try {
            IAccount me = SessionTools.getLoginUser();
            personalAddressExecutor.setDefault(id, me);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }

    @ResponseBody
    @RequestMapping("delete")
    @OperateRecord(value = "个人收货地址:删除", type = OperateType.DELETE)
    public ResponseMessage delete(String[] ids) {
        try {
            IAccount me = SessionTools.getLoginUser();
            personalAddressExecutor.delete(ConvertTools.toList(ids), me);
            return new ResponseMessage();
        } catch (ServiceException e) {
            return new ResponseMessage(e);
        }
    }
}
